// client.h
// client class header file

#ifndef CLIENT_H
#define CLIENT_H

#include <map>
#include <SDL/SDL_net.h>
#include <vector>

class Client;
typedef void (Client::*MessageHandler)(std::vector<std::string>& tokens, 
                                       const char* message, 
                                       const unsigned int sender); 

class Client {
	public:
		Client();
		~Client();
		bool connect(const char*, const unsigned int);
		void disconnect();
		void getMessages();
	
		void setMe(const unsigned int);
		unsigned int getMe() const;
	
		// Message senders 
		void sendDisconnect(const char*);
		void sendChat(const char* chat);
		void sendMove(const unsigned int x, const unsigned int y);
	private:
		unsigned int sendData(const void*, const unsigned int);
		unsigned int receiveData(void*, const unsigned int, const unsigned int);
		
		bool sendMessage(const char* message);
		void invalidMessage(const char* message, const char* reason, 
                            const unsigned int sender);
		void parseMessage(char* message, const unsigned int bufferSize,
			              const unsigned int sender);
		std::map<std::string, MessageHandler, std::less<std::string> >
			messageHandlers_;
		
		// Message handlers
		void getMap(std::vector<std::string>& tokens, const char* message,
		            const unsigned int sender);
		void recvAddFountain(std::vector<std::string>& tokens,
		                     const char* message, const unsigned int sender);
		void recvDisconnect(std::vector<std::string>& tokens,
		                    const char* message, const unsigned int sender);
		void recvNewWizard(std::vector<std::string>& tokens, const char* message,
		                   const unsigned int sender);
		void recvNewClientInfo(std::vector<std::string>& tokens,
		                       const char* message, const unsigned int sender);
		void recvChat(std::vector<std::string>&, const char* message,
		              const unsigned int sender);
		void recvYou(std::vector<std::string>&, const char* message,
		             const unsigned int sender);
		void recvNewTurn(std::vector<std::string>& tokens, const char* message,
				    	 const unsigned int sender);
		void recvNewPhase(std::vector<std::string>& tokens, const char* message,
						  const unsigned int sender);
		void recvNewRound(std::vector<std::string>& tokens, const char* message,
		        		  const unsigned int sender);
		void recvMove(std::vector<std::string>& tokens, const char* message,
		        		  const unsigned int sender);

		TCPsocket serverSocket_;
		SDLNet_SocketSet socketSet_;
		char* messageBuffer_;
		unsigned int messageBufferLen_;
		bool isConnected_;
		unsigned int me_;
		unsigned wizard_;
};


class ClientData {
	public:
		bool isConnected;
		char *name;
};



#endif // CLIENT_H
